{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Greatest Common Divisor of Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math #number-theory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学 #数论"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findGCD"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出数组的最大公约数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> ，返回数组中最大数和最小数的 <strong>最大公约数</strong> 。</p>\n",
    "\n",
    "<p>两个数的&nbsp;<strong>最大公约数</strong> 是能够被两个数整除的最大正整数。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [2,5,6,9,10]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>\n",
    "nums 中最小的数是 2\n",
    "nums 中最大的数是 10\n",
    "2 和 10 的最大公约数是 2\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [7,5,6,8,3]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>\n",
    "nums 中最小的数是 3\n",
    "nums 中最大的数是 8\n",
    "3 和 8 的最大公约数是 1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [3,3]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>\n",
    "nums 中最小的数是 3\n",
    "nums 中最大的数是 3\n",
    "3 和 3 的最大公约数是 3\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-greatest-common-divisor-of-array](https://leetcode.cn/problems/find-greatest-common-divisor-of-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-greatest-common-divisor-of-array](https://leetcode.cn/problems/find-greatest-common-divisor-of-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,5,6,9,10]', '[7,5,6,8,3]', '[3,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return gcd(nums[0], nums[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mx,mn = max(nums),min(nums)\n",
    "        return math.gcd(mx,mn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        minNum = min(nums)\n",
    "        maxNum = max(nums)\n",
    "        res = 0\n",
    "        for i in range(1,minNum+1):\n",
    "            if maxNum % i == 0 and minNum % i == 0:\n",
    "                res = i\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for i in range(nums[0], 0, -1):\n",
    "            if nums[0] % i == 0 and nums[-1] % i == 0:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        \n",
    "        return gcd(max(nums),min(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mx, mn = max(nums), min(nums)\n",
    "        return math.gcd(mx, mn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums_min = min(nums)\n",
    "        nums_max = max(nums)\n",
    "        public_factor = 1\n",
    "        for i in range(1, int(sqrt(nums_min))+1):\n",
    "            if nums_min%i == 0:\n",
    "                if nums_max%(i) == 0 and public_factor < i :\n",
    "                    public_factor = i\n",
    "                if nums_max%(nums_min/i) == 0 and public_factor < nums_min/i:\n",
    "                    public_factor = int(nums_min/i)\n",
    "        return public_factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mina = min(nums)\n",
    "        maxb = max(nums)\n",
    "        if maxb%mina == 0:\n",
    "            return mina\n",
    "        for i in range(mina,0,-1):\n",
    "            if mina%i == 0 and maxb%i == 0:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return gcd(max(nums), min(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "      j = max(nums)\n",
    "      i = min(nums)\n",
    "      temp = i\n",
    "      while temp > 0:\n",
    "        if i % temp == 0 and j % temp == 0:\n",
    "          return temp\n",
    "        temp -= 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mx, mn = max(nums), min(nums)\n",
    "        return math.gcd(mx, mn)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        import math\n",
    "        max_num = max(nums)\n",
    "        min_num = min(nums)\n",
    "        l = []\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(min_num, max_num + 1):\n",
    "                if nums[i] % j == 0:\n",
    "                    l.append(j)\n",
    "        return math.gcd(min(l), max(l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mx, mn = max(nums), min(nums)\n",
    "        return math.gcd(mx, mn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        a=max(nums)\n",
    "        b=min(nums)\n",
    "        lst=[]\n",
    "        for i in range(1,min(a,b)+1):\n",
    "            if a%i==0 and b%i==0:\n",
    "                lst.append(i)\n",
    "        if len(lst)!=0:\n",
    "            return max(lst)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        big=max(nums)\n",
    "        s=min(nums)\n",
    "\n",
    "        if big==s:return s\n",
    "\n",
    "\n",
    "        for i in range(s,0,-1):\n",
    "            if big%i==0 and s%i==0:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        minNum = min(nums)\n",
    "        maxNum = max(nums)\n",
    "        while minNum > 0:\n",
    "            r = maxNum % minNum\n",
    "            maxNum = minNum\n",
    "            minNum = r\n",
    "        return maxNum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums):\n",
    "        mx, mn = max(nums), min(nums)\n",
    "        return math.gcd(mx, mn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        m = max(nums)\n",
    "        n = min(nums)\n",
    "        while n!=0:\n",
    "            m,n=n,m%n\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        minNum = min(nums)\n",
    "        maxNum = max(nums)\n",
    "        res = 0\n",
    "        for i in range(1,minNum+1):\n",
    "            if maxNum % i == 0 and minNum % i == 0:\n",
    "                res = i\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        max=nums[0]\n",
    "        min=nums[0]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]>max:\n",
    "                max=nums[i]\n",
    "            if nums[i]<min:\n",
    "                min=nums[i]\n",
    "        if max%min==0:\n",
    "            return min\n",
    "        rem=1\n",
    "        while min>0:\n",
    "            rem=max%min\n",
    "            max=min\n",
    "            min=rem\n",
    "        return max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return gcd(max(nums), min(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        ans = min_v = min(nums)\n",
    "        max_v = max(nums)\n",
    "        while ans > 1:\n",
    "            st = True\n",
    "            for n in [max_v, min_v]:\n",
    "                if n % ans != 0:\n",
    "                    st = False\n",
    "                    break\n",
    "            if st:\n",
    "                return ans\n",
    "            ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nmax = max(nums)\n",
    "        nmin = min(nums)\n",
    "        r = 1\n",
    "        for i in range(1, nmax+1):\n",
    "            if nmax%i == 0 and nmin%i == 0 and i > r:\n",
    "                r = i\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mx, mn = max(nums), min(nums)\n",
    "        return math.gcd(mx, mn)\n",
    "        #计算两个整数最大公约数 gcd(a,b)的一种常见方法是欧几里得算法，即辗转相除法。其核心部分为：gcd(a,b)=gcd(b,a mod b).\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        minNum = min(nums)\n",
    "        maxNum = max(nums)\n",
    "        res = 0\n",
    "        for i in range(1,minNum+1):\n",
    "            if maxNum % i == 0 and minNum % i == 0:\n",
    "                res = i\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return math.gcd(nums[0],nums[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        a = nums[0]\n",
    "        b = nums[-1]\n",
    "        if b % a == 0:\n",
    "            return a\n",
    "        else:\n",
    "            def gcd(x, y):\n",
    "                if y ==0:\n",
    "                    return x\n",
    "                return gcd(y, x%y)\n",
    "            return gcd(a, b)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        minNum = min(nums)\n",
    "        maxNum = max(nums)\n",
    "        res = 0\n",
    "        for i in range(1,minNum+1):\n",
    "            if maxNum % i == 0 and minNum % i == 0:\n",
    "                res = i\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def GCD(a: int, b: int) -> int:\n",
    "    if a == b: return a\n",
    "    res = 1\n",
    "    for i in range(2, min(a, b)+1):\n",
    "        if a % i == 0 and b % i == 0:\n",
    "            res = i\n",
    "    return res\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return GCD(min(nums), max(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return gcd(min(nums), max(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mx = max(nums)\n",
    "        mn = min(nums)\n",
    "        while mx % mn:\n",
    "            mx, mn = mn, mx % mn\n",
    "        return mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def findGCD(self, nums: List[int]) -> int:\r\n",
    "        max_value = max(nums)\r\n",
    "        min_value = min(nums)\r\n",
    "        for i in range(min_value,0,-1):\r\n",
    "            if max_value % i == 0 and min_value % i == 0:\r\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        max_v = max(nums)\n",
    "        min_v = min(nums)\n",
    "        # print(max_v, min_v)\n",
    "        while min_v > 0:\n",
    "            # max_v, min_v = min_v, max_v % min_v\n",
    "            tmp = min_v\n",
    "            min_v = max_v % min_v\n",
    "            max_v = tmp\n",
    "            # if min_v > tmp:\n",
    "            #     max_v = min_v\n",
    "            #     min_v = tmp\n",
    "            # else:\n",
    "            #     max_v = tmp\n",
    "            # print(max_v, min_v)\n",
    "        return max_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        minNum = min(nums)\n",
    "        maxNum = max(nums)\n",
    "        while minNum > 0:\n",
    "            r = maxNum % minNum\n",
    "            maxNum = minNum\n",
    "            minNum = r\n",
    "        return maxNum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums1 = list(set(nums))\n",
    "        nums1.sort()\n",
    "        minnum,maxnum = nums1[0],nums1[len(nums1) - 1]\n",
    "        p = 1\n",
    "        result = []\n",
    "        while p < minnum + 1:\n",
    "            if minnum%p == 0 and maxnum%p == 0:\n",
    "                result.append(p)\n",
    "            p += 1\n",
    "        return max(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        a, b = min(nums), max(nums)\n",
    "        while a - b:  # 更相消损术\n",
    "            a, b = min(b - a, a), max(b - a, a)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return gcd(min(nums), max(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        max_=0\n",
    "        min_=9999\n",
    "        for i in nums:\n",
    "            if i>max_:\n",
    "                max_=i\n",
    "            if i<min_:\n",
    "                min_=i\n",
    "        return gcd(min_,max_)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        def gcd(a,b):\n",
    "            if a%b == 0:\n",
    "                return b\n",
    "            return gcd(b,a%b)\n",
    "        for idx,num in enumerate(nums):\n",
    "            if idx ==0:\n",
    "                max_number = num\n",
    "                min_number = num\n",
    "            else:\n",
    "                max_number = max(max_number, num)\n",
    "                min_number = min(min_number, num)\n",
    "    # print()\n",
    "    # print(max_number)\n",
    "    # print(min_number)\n",
    "        return gcd(max_number, min_number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        res = 1\n",
    "        print(nums[0], nums[-1])\n",
    "        for i in range(1, nums[-1]+1):\n",
    "            if nums[0] % i == 0 and nums[-1] % i == 0:\n",
    "                res = max(res,i)\n",
    "        return res\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        minNum = min(nums)\n",
    "        maxNum = max(nums)\n",
    "        while True:\n",
    "            temp = minNum\n",
    "            minNum = maxNum % minNum\n",
    "            maxNum = temp\n",
    "            if minNum == 0:\n",
    "                break\n",
    "        return maxNum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        num1, num2 = min(nums), max(nums)\n",
    "        while num2%num1 != 0:\n",
    "            num1, num2 = min(num1, num2%num1), max(num1, num2%num1)\n",
    "        return num1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        zuida = max(nums)\n",
    "        zuixiao = min(nums)\n",
    "        return gcd(zuida,zuixiao)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        min_num, max_num = nums[0], nums[-1]\n",
    "        if max_num % min_num == 0:\n",
    "            return min_num\n",
    "        else:\n",
    "            for i in range(min_num//2+1, 0, -1):\n",
    "                if min_num % i == 0 and max_num % i == 0:\n",
    "                    return i\n",
    "         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return math.gcd(max(nums), min(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        big=-float(\"inf\")\n",
    "        small=float(\"inf\")\n",
    "        for i in nums:\n",
    "            if i>big:\n",
    "                big=i\n",
    "            if i<small:\n",
    "                small=i\n",
    "        \n",
    "        return reduce(gcd,[big,small])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return gcd(max(nums),min(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return gcd(max(nums), min(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        num1, num2 = min(nums), max(nums)\n",
    "\n",
    "        divisor = num1\n",
    "        while divisor > 0:\n",
    "            if num1 % divisor == 0 and num2 % divisor == 0:\n",
    "                return divisor\n",
    "            \n",
    "            divisor -= 1\n",
    "        \n",
    "        return divisor\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        min_num = min(nums)\n",
    "        max_num = max(nums)\n",
    "\n",
    "        if max_num % min_num == 0:\n",
    "            return min_num\n",
    "        return math.gcd(max_num, min_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        max_v = max(nums)\n",
    "        min_v = min(nums)\n",
    "        # print(max_v, min_v)\n",
    "        while min_v > 0:\n",
    "            tmp = min_v\n",
    "            min_v = max_v % min_v\n",
    "            if min_v > tmp:\n",
    "                max_v = min_v\n",
    "                min_v = tmp\n",
    "            else:\n",
    "                max_v = tmp\n",
    "            # print(max_v, min_v)\n",
    "        return max_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums1 = list(set(nums))\n",
    "        nums1.sort()\n",
    "        # minnum,maxnum = nums1[0],nums1[-1]\n",
    "        # p = 1\n",
    "        # result = []\n",
    "        # while p < minnum + 1:\n",
    "        #     if minnum%p == 0 and maxnum%p == 0:\n",
    "        #         result.append(p)\n",
    "        #     p += 1\n",
    "        # return max(result)\n",
    "        return gcd(nums1[0],nums1[-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        a = max(nums)\n",
    "        b = min(nums)\n",
    "\n",
    "        def gcd(a, b):\n",
    "            if b == 0:\n",
    "                return a\n",
    "            else:\n",
    "                return gcd(b, a % b) \n",
    "        return gcd(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        minnum=min(nums)\n",
    "        maxnum=max(nums)\n",
    "        temp=maxnum\n",
    "        while (temp!=0):\n",
    "            temp=maxnum%minnum\n",
    "            maxnum=minnum\n",
    "            minnum=temp\n",
    "        return maxnum\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        import math\n",
    "        a = max(nums)\n",
    "        b = min(nums)\n",
    "        return (math.gcd(a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return math.gcd(min(nums),max(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mx, mn = max(nums), min(nums)\n",
    "        return math.gcd(mx, mn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        maxnum = max(nums)\n",
    "        minnum = min(nums)\n",
    "        return math.gcd(maxnum, minnum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        def gys(x, y):\n",
    "            while x:\n",
    "                tmp = x\n",
    "                x = y%x\n",
    "                y = tmp\n",
    "            return y\n",
    "        if len(nums) <= 1:\n",
    "            return -1\n",
    "        x = nums[0]\n",
    "        y = nums[0]\n",
    "        for i in range(1, len(nums)):\n",
    "            x = min(nums[i], x)\n",
    "            y = max(nums[i], y)\n",
    "        return gys(x, y)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        num1=min(nums)\n",
    "        num2=max(nums)\n",
    "        level=1\n",
    "        for i in range(1,num1+1):\n",
    "            if num1%i==0 and num2%i==0:\n",
    "                level=max(i,level)\n",
    "        return level"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def gcd(self,x,y):\n",
    "        while y:\n",
    "            x,y=y,x%y\n",
    "        return x\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mx,mn=max(nums),min(nums)\n",
    "        return self.gcd(mx,mn)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        l = nums[0]\n",
    "        h = nums[-1]\n",
    "        while l:\n",
    "            t = h%l\n",
    "            h = l\n",
    "            l = t\n",
    "        return h\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        min_num = min(nums)\n",
    "        max_num = max(nums)\n",
    "        i = min_num\n",
    "        while i>0:\n",
    "            if max_num%i == 0 and min_num%i == 0:\n",
    "                return i\n",
    "            i -= 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        f1=min(nums)\n",
    "        f2=max(nums)\n",
    "        v=1\n",
    "        for i in range(1,f1+1):\n",
    "            if f2%i==0 and f1%i==0 and i>v:\n",
    "                v=i\n",
    "        return v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        max=nums[0]\n",
    "        min=nums[0]\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]>max:\n",
    "                max=nums[i]\n",
    "            if nums[i]<min:\n",
    "                min=nums[i]\n",
    "        if max%min==0:\n",
    "            return min\n",
    "        rem=1\n",
    "        while min>0:\n",
    "            rem=max%min\n",
    "            max=min\n",
    "            min=rem\n",
    "        return max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        small, big = nums[0], nums[-1]\n",
    "        ans = 1\n",
    "        if big % small == 0:\n",
    "            return small\n",
    "        for i in range(small, 1, -1):\n",
    "            if big % i == 0 and small % i == 0:\n",
    "                return i\n",
    "\n",
    "        return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        min_num = min(nums)\n",
    "        max_num = max(nums)\n",
    "        i = min_num\n",
    "        while i>0:\n",
    "            # print(i)\n",
    "            if max_num%i == 0 and min_num%i == 0:\n",
    "                return i\n",
    "            i -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return gcd(max(nums), min(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        def gcd(a, b):\n",
    "            return a if b == 0 else gcd(b, a % b)\n",
    "        a = min(nums)\n",
    "        b = max(nums)\n",
    "        return gcd(a, b)     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        \n",
    "        return self.gcd(max(nums), min(nums))\n",
    "                            \n",
    "    def gcd(self, a,b):\n",
    "        while b:\n",
    "            a, b = b, a % b\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        ans = [min(nums),max(nums)]\n",
    "        return reduce(math.gcd,ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        a=max(nums)\n",
    "        b=min(nums)\n",
    "        c=1\n",
    "        if a%b==0:\n",
    "            return b\n",
    "        else:\n",
    "            for i in range(2,b+1):\n",
    "                if a%i==0 and b%i==0:\n",
    "                    c=i\n",
    "            return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        big=max(nums)\n",
    "        s=min(nums)\n",
    "\n",
    "        if big==s:return s\n",
    "\n",
    "\n",
    "        for i in range(s,0,-1):\n",
    "            if big%i==0 and s%i==0:\n",
    "                return i\n",
    "\n",
    "\n",
    "          \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        ln = len(nums)\n",
    "        mn = mx = nums[0]\n",
    "        for i in range(1, ln):\n",
    "            mn = min(nums[i], mn)\n",
    "            mx = max(nums[i], mx)\n",
    "        while mx % mn != 0:\n",
    "            mx, mn = mn, mx % mn\n",
    "        return mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        a = max(nums)\n",
    "        b = min(nums)\n",
    "        aa = []\n",
    "        for i in list(range(1,a+1)):\n",
    "            if a % i == 0 and b % i == 0:\n",
    "                aa.append(i)\n",
    "        return max(aa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        ans = [min(nums),max(nums)]\n",
    "        return reduce(math.gcd,ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        mi,ma=min(nums),max(nums)\n",
    "        while ma%mi:ma,mi=mi,ma%mi\n",
    "        return mi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        return math.gcd(min(nums),max(nums))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findGCD(self, nums: List[int]) -> int:\n",
    "        maxs=max(nums)\n",
    "        mins=min(nums)\n",
    "        \n",
    "        re=maxs\n",
    "        while re !=0:\n",
    "            re = maxs % mins\n",
    "            maxs=mins\n",
    "            mins=re\n",
    "        return maxs"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
